﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using SqlServer = DataCoreTechnologies.SqlServer;
using Business = DataCoreTechnologies.Scribe.Business;
using Html = DataCoreTechnologies.Scribe.Business.Html;

namespace DataCoreTechnologies.Scribe
{
	internal class MainFormBusiness : Form
	{
		//Properties
		protected SqlServer.Server _server;

		//Methods
      protected SqlConnectionStringBuilder GetSqlConnectionStringBuilder()
      {
         ConnectionForm serverConn;
         if (_server == null)
            serverConn = new ConnectionForm(null);
         else
            serverConn = new ConnectionForm(_server.ConnectionStringBuilder);

         if (serverConn.ShowDialog() == DialogResult.OK)
            return serverConn.ConnectionStringBuilder;
         else
            return null;
      }

      protected List<Business.Page> _pages = new List<Business.Page>();
		protected Business.Page _activePage { get; set; }
		protected void SetActivePage(String url)
		{
			foreach (Business.Page page in _pages)
			{
				if (page.URL == url)
				{
					_activePage = page;
					return;
				}
			}
		}
		protected void SetActivePage(TreeNode node)
		{
			foreach (Business.Page page in _pages)
			{
				if (page.treeNode == node)
				{
					_activePage = page;
					return;
				}
			}
		}
		protected void AddPage(TreeNode node, Html.Document document)
		{
			_pages.Add(new Business.Page()
			{
				htmlDocument = document,
				treeNode = node,
				URL = (@"http://" + document.URL).ToLower()
			});
		}
		protected void ClearPages(SqlServer.Database database) 
		{
			if (database == null)
				_pages.Clear();
			else
			{
            _pages.RemoveAll(page => page.htmlDocument.RootDatabase == database
                                    && page.htmlDocument.DocumentType != Html.DocumentType.Database);
			}
		}
      protected void InvalidatePages(SqlServer.Database database)
      {
         if (database == null)
         {
            foreach (Business.Page page in _pages)
            {
               page.htmlDocument.Invalidate();
            }
         }
         //Used by refresh database process
         else
         {
            foreach (Business.Page page in _pages)
            {
               if (page.htmlDocument.RootDatabase == database) page.htmlDocument.Invalidate();
            }
         }
      }
      protected Html.Document GetHtmlDocument(TreeNode node)
		{
			foreach (Business.Page page in _pages)
			{
				if (page.treeNode == node) return page.htmlDocument;
			}
			return null;
		}
		protected Html.Document GetHtmlDocument(String url)
		{
			foreach (Business.Page page in _pages)
			{
				if (page.URL == url) return page.htmlDocument;
			}
			return null;
		}
		protected TreeNode GetTreeNode(Html.Document document)
		{
			foreach (Business.Page page in _pages)
			{
				if (page.htmlDocument == document) return page.treeNode;
			}
			return null;
		}
		protected TreeNode GetTreeNode(String url)
		{
			foreach (Business.Page page in _pages)
			{
				if (page.URL == url) return page.treeNode;
			}
			return null;
		}
		protected IEnumerable<Business.Page> Pages
		{
			get
			{
				foreach (Business.Page page in _pages)
				{
					yield return page;
				}
			}
		}
	}
}
